
#region Imports

using System;
using System.Collections.Generic;

using Justa.Aspect;

#endregion

namespace Justa.Reflection
{
    /// <summary>
    /// 
    /// </summary>
    /// 
    /// <author>Abel Perez</author>
    /// 
    public class ProxyFactory
    {
        /// <summary>
        /// 
        /// </summary>
        /// 
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="K"></typeparam>
        /// 
        /// <param name="methodInvoker"></param>
        /// 
        /// <returns></returns>
        /// 
        public static T CreateProxy<T, K>(MethodInvoker methodInvoker)
        {
            object target = Activator.CreateInstance<K>();
            return CreateProxy<T>(target, methodInvoker);
        }
        
        /// <summary>
        /// 
        /// </summary>
        /// 
        /// <typeparam name="T"></typeparam>
        /// 
        /// <param name="target"></param>
        /// <param name="methodInvoker"></param>
        /// 
        /// <returns></returns>
        /// 
        public static T CreateProxy<T>(object target, MethodInvoker methodInvoker)
        {
            if (methodInvoker == null)
            {
                throw new ArgumentException("MethodInvoker is null.");
            }

            InvocationHandler handler = new InvocationHandler(target, typeof(T));
            handler.invoker += methodInvoker;

            T result = (T)handler.GetTransparentProxy();
            return result;
        }
    }
}
